#include "widget.h"
#include "ui_widget.h"
#include "mainwindow.h"
#include <QKeySequence>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    QPixmap image(":/image/logo.png");
    ui->imageLabel->setPixmap(image);
    ui->imageLabel->setScaledContents(true);
    QPixmap image_background(":/image/background.jpeg"); //设置登录界面的背景，使其自适应窗口大小
    ui->label_background->setPixmap(image_background);
    ui->label_background->setScaledContents(true);
    ui->connectButton->setShortcut(QKeySequence("Return"));//回车快捷键
    socket=new QTcpSocket;
}
Widget::~Widget()
{
    delete ui;
}
void Widget::on_cancelButton_clicked()
{
    Widget::close();
}


void Widget::on_connectButton_clicked()
{
    //获取ip和端口号
    QString ip =ui->ipedit->text();
    QString port =ui->portedit->text();
    //连接服务器
    socket->abort();
    socket->connectToHost(QHostAddress(ip),port.toShort());
    //发出信号
    connect(socket,&QTcpSocket::connected,this,&Widget::report_connect_success);
    //连接断开
    connect(socket,&QTcpSocket::disconnected,this,&Widget::report_disconnect_success);

}
void Widget::report_connect_success()
{
    QSound *soundEffect = new QSound( ":/sound/success.wav");
    soundEffect->play();
    QMessageBox::information(this,"提示","连接成功");
    this->hide();
    mainwindow* m=new mainwindow(socket);
    m->show();
}
void Widget::report_disconnect_success()
{
   QMessageBox::information(this,"提示","连接失败");
}
